package com.vm.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.vm.commons.StringUtil;
import com.vm.commons.VMConst;
import com.vm.model.AppType;
import com.vm.utils.MyDisplayImageOptions;
import com.wwtscapp.apk.R;

public class CategoryAppListAdapter extends BaseAdapter {

	public static View convertView;
	private Context mContext;
	private List<AppType> apptypes;
	List<AppType> gridtype;
	List<AppType> childTypes;

	/**
	 * 
	 */
	public CategoryAppListAdapter(Context context, List<AppType> apptypes) {
		this.mContext = context;
		this.apptypes = apptypes;

	}

	public void setChildTypes(List<AppType> childTypes) {
		this.childTypes = childTypes;
	}

	@Override
	public int getCount() {
		return apptypes.size();
	}

	@Override
	public Object getItem(int position) {
		return null;
	}

	@Override
	public long getItemId(int position) {
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		gridtype = new ArrayList<AppType>();
		ViewHolder viewHolder = null;
		if (convertView == null) {

			convertView = LayoutInflater.from(mContext).inflate(
					R.layout.category_apply_item, null);
			viewHolder = new ViewHolder();

			viewHolder.tv_big_category = (TextView) convertView
					.findViewById(R.id.tv_big_category);
			viewHolder.gridView1 = (GridView) convertView
					.findViewById(R.id.gridView1);
			viewHolder.iv_big_category = (ImageView) convertView
					.findViewById(R.id.iv_big_category);

			convertView.setTag(viewHolder);
		} else {
			viewHolder = (ViewHolder) convertView.getTag();
		}

		AppType apptype = apptypes.get(position);
		if (apptype.getId() != null) {
			viewHolder.tv_big_category.setTag(apptype.getId());
		}
		if (apptype.getName() != null) {
			viewHolder.tv_big_category.setText(StringUtil.getShotName(apptype
					.getName()));
		}
		// for(int i=0;i<apptype.getChildTypes().size();i++){
		// gridtype.add(apptype);
		// }
		gridtype = apptype.getChildTypes();
		if (gridtype != null) {
			CategoryAppGridAdapter myAdapter = new CategoryAppGridAdapter(
					mContext, gridtype);
			viewHolder.gridView1.setAdapter(myAdapter);
		}
		String url = VMConst.URL + apptype.getImageUrl();
		ImageLoader.getInstance().displayImage(url, viewHolder.iv_big_category,
				MyDisplayImageOptions.getInstance());

		gridtype = null;
		return convertView;

	}

	static class ViewHolder {
		TextView tv_big_category;
		GridView gridView1;
		List<AppType> gridtypes;
		ImageView iv_big_category;
	}

}
